package com.huike.web.controller.review;


import com.huike.business.domain.TbBusiness;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 添加
     * @param name
     * @param age
     * @param sex
     * @return
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData(@PathVariable String name, @PathVariable int age, @PathVariable  String sex, TbBusiness tbBusiness) {
        tbBusiness.setCreateBy(SecurityUtils.getUsername());
        Date now= DateUtils.getNowDate();
        tbBusiness.setCreateTime(now);
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        return toAjax(reviewService.add(review));
    }

    /**
     * 添加
     * @param review
     * @return
     */
    @PostMapping("/saveData")
    public AjaxResult save(@RequestBody Review review) {
        return toAjax(reviewService.add(review));

    }

    /**
     * 修改
     * @param review
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        return toAjax(reviewService.update(review));
    }
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable long id){
        if(reviewService.delete(id)==1){
            return AjaxResult.success("成功删除:1条数据");
        } else {
            throw new CustomException("你删个dre!给爷噶!");
        }
    }
    @GetMapping("/getById")
    public AjaxResult get(long id){
        List<Review> list= reviewService.getById(id);
        return AjaxResult.success("操作成功",list);
    }
    @GetMapping("/getDataByPage")
    public TableDataInfo page(){
        List<Review> reviews =reviewService.page();
        return getDataTablePage(reviews);
    }

    /**=========================================================新增数据============================================*/

    /**=========================================================删除数据=============================================*/

    /**=========================================================修改数据=============================================*/

    /**=========================================================查询数据=============================================*/

}